package com.buba.cargo.controller;

import com.buba.cargo.pojo.OrderForm;
import com.buba.cargo.service.OrderFormService;
import com.buba.customer.service.CustomerService;
import com.buba.goods.service.MatterService;
import com.buba.system.pojo.Employee;
import com.buba.system.pojo.Job;
import com.buba.system.pojo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/OrderForm")
public class OrderFormController {
    private OrderFormService orderFormService;
    private MatterService matterService;
    private CustomerService customerService;

    /**
     * 添加方法
     * @param map
     * @return
     */
    @RequestMapping("/addOrderForm")
    public Map<String,Object> addOrderForm(@RequestParam Map<String,Object> map){
        //获取时间戳
        Long sj= System.currentTimeMillis();
        String XHOrderNo = sj+"";
        map.put("XHOrderNo",XHOrderNo);
        //获取物质价格
        Integer con= matterService.selMatterPriceByNo(map);
        Integer s= Integer.parseInt(map.get("ItemsPrice").toString());
        Integer ItemsProfitPrice=s-con;
        map.put("ItemsProfitPrice",ItemsProfitPrice);
        Integer count=orderFormService.addOrderForm(map);
        if(count==1){
            map.put("message","添加成功");
        }else{
            map.put("message","添加失败");
        }
        return  map;
    }
    //批量删除
    @RequestMapping("/delOrderForm")
    public Map<String,Object> delOrderForm(String ids){
        Map<String,Object> map=new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        Integer count=orderFormService.delOrderForm(list);
        if(count!=null){
            map.put("message","删除"+count+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }
    @RequestMapping("/selOrderForm")
    public Page selOrderForm(@RequestParam Map<String,Object> map, HttpSession session){
        Page page = new Page((Integer.parseInt(map.get("pageNow").toString())),(Integer.parseInt(map.get("pageCount").toString())));
        //获取session信息
        Employee employee = (Employee) session.getAttribute("employee");
        List<Job> jobList=employee.getJobList();
        for (Job j: jobList) {
            if("财务员".equals(j.getJobTitle())){
                //查询条数
                page.computePageTotal(orderFormService.selOrderFormCount(map));
                //查询信息
                List<OrderForm> list = orderFormService.selOrderForm(map);
                //遍历集合返回信息
                for(OrderForm o : list){
                    //重新设置客户信息
                  String cc=  customerService.selCustomerById( o.getCustomerNo());
                  o.setCompanyName(cc);
                }
                //设置信息
                page.setList(list);
                //用于前端判断返回的是业务员还是财务员
                page.setZxc(1);
            }
            if("业务员".equals(j.getJobTitle())){
                map.put("employeeId",employee.getEmployeeId());
                //查询条数
                page.computePageTotal(orderFormService.selOrderFormCount(map));
                //查询信息
                List<OrderForm> list = orderFormService.selOrderForm(map);
                //遍历集合返回信息
                for(OrderForm o : list){
                    //重新设置客户信息
                    String cc=  customerService.selCustomerById( o.getCustomerNo());
                    o.setCompanyName(cc);
                }
                //设置信息
                page.setList(list);
                page.setZxc(2);
            }

        }

        return page;
    }
    @RequestMapping("/updOrderFormByshenhe")
    public Map<String,Object> updOrderFormByshenhe(@RequestParam Map<String,Object> map,HttpSession session){
        //获取session信息
        Employee employee = (Employee) session.getAttribute("employee");
        //审核时自动存入财务员编号
        map.put("employeeId",employee.getEmployeeId());
        Integer count=orderFormService.updOrderFormByshenhe(map);
        if(count==1){
            map.put("message","审核成功");
        }else{
            map.put("message","审核失败");
        }
        return map;
    }
    @Autowired
    @Qualifier("orderFormImpl")
    public void setOrderFormService(OrderFormService orderFormService) {
        this.orderFormService = orderFormService;
    }
    @Autowired
    @Qualifier("matterServiceImpl")
    public void setMatterService(MatterService matterService) {
        this.matterService = matterService;
    }
    @Autowired
    @Qualifier("customerServiceImpl")
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
}
